package com.plat.net.dns;

import com.plat.net.tcp.TcpServer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DnsServer {
    Logger logger = LoggerFactory.getLogger(DnsServer.class);

    private NioEventLoopGroup group;

    private Bootstrap bootstrap;

    public DnsServer() {
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
    }

    public void start(int port) {
        bootstrap.group(group).channel(NioDatagramChannel.class)
                .handler(new DnsServerChannelInitializer())
                .option(ChannelOption.SO_BROADCAST, true).bind(port).syncUninterruptibly()
                .addListener(future -> {
                    if (future.isSuccess()) {
                        if (logger.isInfoEnabled()) {
                            logger.info("DnsServer start success：port：" + port);
                        }
                    } else {
                        if (logger.isErrorEnabled()) {
                            logger.error("DnsServer start fail ：port：" + port);
                        }
                    }
                });
    }

    public static void main(String[] args) {
        DnsServer server = new DnsServer();
        server.start(53);
    }
}
